package com.UnionPayCenter.service.serviceImpl;

import com.UnionPayCenter.entity.*;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.*;
import com.UnionPayCenter.service.IChannelService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: bite
 * @Date: 2019/4/25
 */
@Service
@Transactional
public class ChannelServiceImpl implements IChannelService {
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public List<ChannelEntity> findList(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            String name = jsonParam.getString("name");
            List<ChannelEntity> list = channelMapper.selectList(shopId, name);
            return list;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public boolean add(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {

            String name = jsonParam.getString("name");
            String shortName = jsonParam.getString("shortName");

            List<ChannelEntity> list  = channelMapper.selectByName(shopId, name);
            if(list != null && list.size() > 0){
                throw new ApiException(ErrorCodeEnum.CE401);
            }
            ChannelEntity channelEntity = new ChannelEntity();
            channelEntity.setName(name);
            channelEntity.setShortName(shortName);
            channelEntity.setType(1);
            channelEntity.setShopId(shopId);
            channelMapper.insertSelective(channelEntity);
            return true;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public boolean update(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {

            Integer channelId = jsonParam.getInteger("channelId");
            String name = jsonParam.getString("name");
            String shortName = jsonParam.getString("shortName");

            List<ChannelEntity> list  = channelMapper.selectByName(shopId, name);
            if(list != null && list.size() > 0){
                for (ChannelEntity channel : list) {
                    if(channel.getChannelId() != channelId){
                        throw new ApiException(ErrorCodeEnum.CE401);
                    }
                }
            }
            ChannelEntity channelEntity = new ChannelEntity();
            channelEntity.setChannelId(channelId);
            channelEntity.setName(name);
            channelEntity.setShortName(shortName);
            channelMapper.updateByPrimaryKeySelective(channelEntity);
            return true;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }
}
